clear all
import excel "C:\Users\mdebus\Dropbox\ethnic_party_positions_immigration\Replication material\10082022_dataset_full.xlsx", sheet("10082022_dataset_full") firstrow


* dep var

gen tmp = subinstr(per607,",",".",.)
replace tmp="." if tmp=="NA"
destring tmp, replace
tab tmp
drop per607
rename tmp per607
tab per607

gen tmp = subinstr(rile,",",".",.)
replace tmp="." if tmp=="NA"
destring tmp, replace
tab tmp
drop rile
rename tmp rile
tab rile

gen tmp = subinstr(per608,",",".",.)
replace tmp="." if tmp=="NA"
destring tmp, replace
tab tmp
drop per608
rename tmp per608
tab per608

gen tmp = subinstr(per601,",",".",.)
replace tmp="." if tmp=="NA"
destring tmp, replace
tab tmp
drop per601
rename tmp per601
tab per601

gen tmp = subinstr(per602,",",".",.)
replace tmp="." if tmp=="NA"
destring tmp, replace
tab tmp
drop per602
rename tmp per602
tab per602

gen multiculti = per607-per608
tab multiculti, mis

gen national = per601-per602
tab national, mis

gen tmp = subinstr(per705,",",".",.)
replace tmp="." if tmp=="NA"
destring tmp, replace
tab tmp
drop per705
rename tmp per705
tab per705

gen tmp = subinstr(Ethnic_fract, ",", ".",.)
replace tmp="." if tmp=="NA"
destring tmp, replace
tab tmp
drop Ethnic_fract
rename tmp ethnic_fract
tab ethnic_fract

gen tmp = subinstr(GDPpc, ",", ".",.)
replace tmp="." if tmp=="NA"
destring tmp, replace
tab tmp
drop GDPpc
rename tmp gdppc
tab gdppc

tab LibDemocr, mis

gen tmp = subinstr(LibDemocr, ",", ".",.)
replace tmp="." if tmp=="NA"
destring tmp, replace
tab tmp
drop LibDemocr
rename tmp libdemocr
tab libdemocr


* gen country-date as group

gen tmp = string(edate)
gen country_election = countryname+tmp

gen eco = 0
replace eco = 1 if parfam==10

gen com = 0
replace com = 1 if parfam==20

gen soc = 0
replace soc = 1 if parfam==30

gen lib = 0
replace lib = 1 if parfam==40

gen chr = 0
replace chr = 1 if parfam==50

gen con = 0 
replace con = 1 if parfam==60

gen nat = 0
replace nat = 1 if parfam==70

gen agr = 0
replace agr = 1 if parfam==80

gen sip = 0
replace sip = 1 if parfam==90

tab ethnic_party sip


tab libdemocr

tab number_direct_competitors, mis
gen direct_competitors=0
replace direct_competitors=1 if number_direct_competitors>0

tab direct_competitors ethnic_party 
tab ethnic_party ethnic_competitor

tab ethnic_party direct_competitor

tab eumember

gen ethnic_party_competition = 0
replace ethnic_party_competition = 1 if ethnic_party==1 & direct_competitor==1

gen indirect_ethnic_party_comp = 0
replace indirect_ethnic_party_comp=1 if ethnic_competitor==1

tab ethnic_party_competition indirect_ethnic_party_comp

gen gdppc_1000 = gdppc/1000

tab ethnic_competitor

tab direct_competitor
tab direct_competitor ethnic_competitor
tab number_direct_competitors direct_competitor


keep if ethnic_party==1


tab Numeric_minority, mis
replace Numeric_minority="" if Numeric_minority=="NA"

destring Numeric_minority, replace

tab Powerful, mis
replace Powerful="" if Powerful=="NA"
destring Powerful, replace


drop tmp
gen tmp = subinstr(markeco,",",".",.)
replace tmp="." if tmp=="NA"
destring tmp, replace
tab tmp
drop markeco
rename tmp markeco
tab markeco

exit

* descriptives


twoway (scatter multiculti date if year>1960, sort)
graph box multiculti, over(parfam)

twoway (scatter multiculti rile if date>1960, sort)

twoway (scatter multiculti markeco if date>1960, sort)
 
twoway (scatter multiculti ethnic_competitor if date>1960, sort)
twoway (scatter multiculti direct_competitors if date>1960, sort)

twoway (scatter multiculti number_direct_competitors if date>1960, sort)
	
	

*analysis 


*EC/EU states
eststo clear
* Model 1
eststo: reg multiculti i.ethnic_competitor  ///
 year ///
 Numeric_minority ///
 rile ///
 eco com soc lib chr con nat agr sip ///
ethnic_fract gdppc libdemocr if eumember==10, cluster(country_election)

* Model 2
eststo: reg multiculti direct_competitors  ///
 year ///
 Numeric_minority ///
 rile ///
  eco com soc lib chr con nat agr sip ///
ethnic_fract gdppc libdemocr if eumember==10, cluster(country_election)

* Model 3
eststo: reg multiculti number_direct_competitors  ///
 year ///
 rile ///
  Numeric_minority ///
  eco com soc lib chr con nat agr sip ///
ethnic_fract gdppc libdemocr if eumember==10, cluster(country_election)

* Model 4
eststo: reg multiculti i.ethnic_competitor##c.rile  ///
 year ///
  Numeric_minority ///
 eco com soc lib chr con nat agr sip ///
ethnic_fract gdppc libdemocr if eumember==10, cluster(country_election)

margins, at(rile=(-70(20)90) ethnic_competitor=(0 1))
marginsplot, level(90) x(rile) recast(line) xlabel(-70(20)90) 

* Model 5
eststo: reg multiculti i.direct_competitors##c.rile  ///
 year ///
  Numeric_minority ///
  eco com soc lib chr con nat agr sip ///
ethnic_fract gdppc libdemocr if eumember==10, cluster(country_election)

margins, at(rile=(-70(20)90) direct_competitors=(0 1))
marginsplot, level(90) x(rile) recast(line) xlabel(-70(20)90) 
 
* Model 6
eststo: reg multiculti c.number_direct_competitors##c.rile  ///
 year ///
  Numeric_minority ///
  eco com soc lib chr con nat agr sip ///
ethnic_fract gdppc libdemocr if eumember==10, cluster(country_election)

margins, at(rile=(-70(20)90) number_direct_competitors=(0(2)4))
marginsplot, level(90) x(rile) recast(line) xlabel(-70(20)90) 


* additional tests - markeco position of parties as explanatory variable
reg multiculti c.number_direct_competitors##c.markeco  ///
 year ///
  eco com soc lib chr con nat agr sip ///
ethnic_fract gdppc libdemocr if eumember==10, cluster(country_election)


reg multiculti c.number_direct_competitors##c.Powerful  ///
 year ///
  eco com soc lib chr con nat agr sip ///
ethnic_fract gdppc libdemocr if eumember==10, cluster(country_election)

reg multiculti c.number_direct_competitors##c.rile  ///
 year ///
 Numeric_minority ///
 Powerful ///
  eco com soc lib chr con nat agr sip ///
ethnic_fract gdppc libdemocr if eumember==10, cluster(country_election)


reg multiculti c.number_direct_competitors##c.Numeric_minority  ///
 year ///
  eco com soc lib chr con nat agr sip ///
ethnic_fract gdppc libdemocr if eumember==10, cluster(country_election)


esttab using "C:\Users\mdebus\Dropbox\ethnic_party_positions_immigration\ANALYSIS\models_11Aug2022_eu.rtf" , replace ///
	abs b(3) se(3) ///
	star(+ 0.10 * 0.05 ** 0.01) ///
	aic ///
	scalars("ll Log pseudolikelihood") ///
	nonumbers /// 
	coeflabels(_cons "Constant" ///
	ethnic_party "Ethnic party" ///
	ethnic_competitor "Competing ethinc party in election" ///
	eco "Green parties" ///
	com "Communist and left-socialist parties" ///
	soc "Social democratic parties" ///
	lib "Liberal parties" ///
	chr "Christian democratic parties" ///
	rile "Right-left position (rile)" ///
	con "Conservative parties" ///
	nat "Nationalist parties" ///
	agr "Agrarian parties" ///
	Numeric_minority "Minority" ///
	sip "Special interest parties" ///
	year "Time trend" ///
	ethnic_fract "Ethnic fractionlisation" ///
	gdppc "GDP per capita") ///
	nonotes addnotes("Note: The dependent variable is the parties' position on multiculturalism. Numbers in parentheses are robust standard errors clustered by country and election. Significance levels: + = p = 0.1; * = p = 0.05; ** = p = 0.01.") 
	

* analyses for all OECD states
* Model 1

eststo clear 
eststo: reg multiculti ethnic_competitor  ///
 year ///
   Numeric_minority ///
 rile ///
 eco com soc lib chr con nat agr sip ///
ethnic_fract gdppc libdemocr if oecdmember==10, cluster(country_election)

* Model 2
eststo: reg multiculti direct_competitors  ///
 year ///
   Numeric_minority ///
 rile ///
  eco com soc lib chr con nat agr sip ///
ethnic_fract gdppc libdemocr if oecdmember==10, cluster(country_election)

* Model 3
eststo: reg multiculti number_direct_competitors  ///
 year ///
   Numeric_minority ///
 rile ///
  eco com soc lib chr con nat agr sip ///
ethnic_fract gdppc libdemocr if oecdmember==10, cluster(country_election)

* Model 4
eststo: reg multiculti i.ethnic_competitor##c.rile  ///
 year ///
   Numeric_minority ///
 eco com soc lib chr con nat agr sip ///
ethnic_fract gdppc libdemocr if oecdmember==10, cluster(country_election)

margins, at(rile=(-70(20)90) ethnic_competitor=(0 1))
marginsplot, level(90) x(rile) recast(line) xlabel(-70(20)90) 

* Model 5
eststo: reg multiculti i.direct_competitors##c.rile  ///
 year ///
   Numeric_minority ///
  eco com soc lib chr con nat agr sip ///
ethnic_fract gdppc libdemocr if oecdmember==10, cluster(country_election)

margins, at(rile=(-70(20)90) direct_competitors=(0 1))
marginsplot, level(90) x(rile) recast(line) xlabel(-70(20)90) 

* MOdel 6
eststo: reg multiculti c.number_direct_competitors##c.rile  ///
 year ///
   Numeric_minority ///
  eco com soc lib chr con nat agr sip ///
ethnic_fract gdppc libdemocr if oecdmember==10, cluster(country_election)

margins, at(rile=(-70(20)90) number_direct_competitors=(0(2)4))
marginsplot, level(90) x(rile) recast(line) xlabel(-70(20)90) 


esttab using "C:\Users\mdebus\Dropbox\ethnic_party_positions_immigration\ANALYSIS\models_11August2022_oecd.rtf" , replace ///
	abs b(3) se(3) ///
	star(+ 0.10 * 0.05 ** 0.01) ///
	aic ///
	scalars("ll Log pseudolikelihood") ///
	nonumbers /// 
	coeflabels(_cons "Constant" ///
	ethnic_party "Ethnic party" ///
	ethnic_competitor "Competing ethinc party in election" ///
	eco "Green parties" ///
	com "Communist and left-socialist parties" ///
	soc "Social democratic parties" ///
	lib "Liberal parties" ///
	chr "Christian democratic parties" ///
	rile "Right-left position (rile)" ///
	con "Conservative parties" ///
	nat "Nationalist parties" ///
	agr "Agrarian parties" ///
	sip "Special interest parties" ///
		Numeric_minority "Minority" ///
	year "Time trend" ///
	ethnic_fract "Ethnic fractionlisation" ///
	gdppc "GDP per capita") ///
	nonotes addnotes("Note: The dependent variable is the parties' position on multiculturalism. Numbers in parentheses are robust standard errors clustered by country and election. Significance levels: + = p = 0.1; * = p = 0.05; ** = p = 0.01.") 

	
***markeco as explanatory variable

eststo: reg multiculti i.ethnic_competitor  ///
 year ///
 Numeric_minority ///
 markeco ///
 eco com soc lib chr con nat agr sip ///
ethnic_fract gdppc libdemocr if eumember==10, cluster(country_election)

* Test of hypothesis 1 (second part) and hypothesis 2

eststo: reg multiculti direct_competitors  ///
 year ///
 Numeric_minority ///
 markeco ///
  eco com soc lib chr con nat agr sip ///
ethnic_fract gdppc libdemocr if eumember==10, cluster(country_election)


eststo: reg multiculti number_direct_competitors  ///
 year ///
 markeco ///
  Numeric_minority ///
  eco com soc lib chr con nat agr sip ///
ethnic_fract gdppc libdemocr if eumember==10, cluster(country_election)

* Test of hypothesis 3 - Interaction term between ethnic competitors and left-right orientation

eststo: reg multiculti i.ethnic_competitor##c.markeco  ///
 year ///
  Numeric_minority ///
 eco com soc lib chr con nat agr sip ///
ethnic_fract gdppc libdemocr if eumember==10, cluster(country_election)

margins, at(markeco=(0(5)25) ethnic_competitor=(0 1))
marginsplot, level(90) x(markeco) recast(line) xlabel(0(5)25) 


eststo: reg multiculti i.direct_competitors##c.markeco  ///
 year ///
  Numeric_minority ///
  eco com soc lib chr con nat agr sip ///
ethnic_fract gdppc libdemocr if eumember==10, cluster(country_election)

margins, at(markeco=(0(5)25) direct_competitors=(0 1))
marginsplot, level(90) x(markeco) recast(line) xlabel(0(5)25) 
 

eststo: reg multiculti c.number_direct_competitors##c.markeco  ///
 year ///
  Numeric_minority ///
  eco com soc lib chr con nat agr sip ///
ethnic_fract gdppc libdemocr if eumember==10, cluster(country_election)

margins, at(markeco=(0(5)25) number_direct_competitors=(0(2)4))
marginsplot, level(90) x(markeco) recast(line) xlabel(0(5)25) 


**************************************nationalism as dep variable

****EC/EU states
eststo clear
* Model 1

eststo: reg national ethnic_competitor  ///
 year ///
 Numeric_minority ///
 eco com soc lib chr con nat agr sip ///
ethnic_fract gdppc libdemocr if eumember==10, cluster(country_election)

* Model 2

eststo: reg national direct_competitors  ///
 year ///
 Numeric_minority ///
  eco com soc lib chr con nat agr sip ///
ethnic_fract gdppc libdemocr if eumember==10, cluster(country_election)

* Model 3
eststo: reg national number_direct_competitors  ///
 year ///
  Numeric_minority ///
  eco com soc lib chr con nat agr sip ///
ethnic_fract gdppc libdemocr if eumember==10, cluster(country_election)



